package com.study.services;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IComplaintDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDeptDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.IGoodDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IRepairlistDAO;
import com.study.model.jdao.IReturneDAO;
import com.study.model.jdao.IServiceDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.DeportFormMapper;
import com.study.model.mdao.GoodDAO;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.OrderFormDetailMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.mdao.ReturnmoreMapper;
import com.study.model.mdao.TousuDAO;
import com.study.model.mdao.WeixiuDAO;
import com.study.model.pojo.Complaint;
import com.study.model.pojo.Good;
import com.study.model.pojo.Repairlist;
import com.study.model.pojo.Service;

@org.springframework.stereotype.Service
@Transactional(rollbackOn = Exception.class)
public class WeixiuService {

	@Autowired
	WeixiuDAO mapper;
	@Autowired
	ILinkmanDAO linkmanDAO;
	@Autowired
	ICustomerDAO customerDAO;
	@Autowired
	IUserDAO userDAO;
	@Autowired
	IDictionaryDAO dictionaryDAO;
	@Autowired
	IRepairlistDAO dao;
	@Autowired
	GoodDAO goodDAO;
	@Autowired
	IGoodDAO iGoodDAO;
	@Autowired
	IDeptDAO deptDAO;
	@Autowired
	OrderFormMapper oFormMapper;// 订单
	@Autowired
	ReturnmoreMapper returneMapper;// 退货
	@Autowired
	OrderFormDetailMapper detailMapper;// 订单详情
	@Autowired
	DeportFormMapper deportFormMapper;// 库存表
	@Autowired
	GoodDAO gDao;
	@Autowired
	LogforcustDAO logforcustDAO;

	/**
	 * 根据条件查询售后服务
	 * @param cusid 
	 * 
	 * @param i
	 * @param j
	 * @param string
	 * @param string2
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects, Integer cusid) {
		String type = null;
		String cat = null;
		String state = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("40")) {
			cat = selects;
		} else if (selects.length() > 0 && selects.startsWith("45")) {
			type = selects;
		} else if (selects.length() > 0 && selects.startsWith("46")) {
			state = selects;
		}
		return mapper.selectByWhere(type, cat, state, "%" + input + "%",cusid);
	}

	/**
	 * 根据id查询一条记录
	 * 
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		return mapper.selectById(parameter);
	}

	/**
	 * 删除维修工单
	 * 
	 * @param goo_id
	 */
	public void delete(int cid) {
		// 只进行逻辑删除
		Repairlist repairlist = dao.findByRepId(cid);
		repairlist.setIsShow(0);
	}

	/**
	 * 编辑
	 * 
	 * @param theme
	 * @param user
	 * @param type
	 * @param dates
	 * @param times
	 * @param custname
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param linkman
	 * @param more
	 * @param id
	 * @return
	 */
	public boolean editorService(String id, String phone, String cat, String state, String desc, String more,
			Date comdate) {
		Repairlist repairlist = dao.findByRepId(Integer.parseInt(id));
		repairlist.setDictionary2(dictionaryDAO.findByDicId(Integer.parseInt(state)));
		repairlist.setDictionary3(dictionaryDAO.findByDicId(Integer.parseInt(cat)));
		repairlist.setRepUniontime(comdate);
		repairlist.setRepDescribe(desc);
		repairlist.setRepPhone(phone);
		repairlist.setRepRemark(more);
		return true;
	}

	/**
	 * 添加
	 * 
	 * @param name
	 * @param type
	 * @param dept
	 * @param custid
	 * @param date1
	 * @param user
	 * @param linId
	 * @param phone
	 * @param money
	 * @param cat
	 * @param repair
	 * @param address
	 * @param isTimeout
	 * @param state
	 * @return
	 */
	public boolean addWeixiu(String theme, String custid, String goodid, String order, Date date1, String dept,
			String user, String linId, String phone, String money, String type, String address, String desc,
			String useid, String isTimeout,String custname) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String date = format.format(date1);
		Integer cat = null;
		if ("该商品在保质期内。".equals(isTimeout)) {
			cat = 401;// 保修
		} else {
			cat = 402;// 出保
		}

		int row = mapper.insert(theme, custid, goodid, order, date, dept, user, linId, phone, money, type, address,
				desc, useid, new Date(), cat);
		
		Integer id = Integer.parseInt(custid);
		Integer useId = Integer.parseInt(useid);
		//添加一条流水
		String logMessage = "客户维修&nbsp;&nbsp;"+theme +"===="+custname;
		logforcustDAO.insert(id, new Date(), useId, 227, logMessage, "");
		return row > 0 ? true : false;
	}

	/**
	 * 维修状态统计
	 * 
	 * @return
	 */
	public List<JSONObject> selectByResult() {
		return mapper.selectByResult();
	}

	/**
	 * 查询所有商品
	 * 
	 * @return
	 */
	public List<JSONObject> findGoods() {

		 return goodDAO.selectAllByPeiJian();
		//return goodDAO.selectAll();
	}

	/**
	 * 根据custid查询对应的订单
	 * 
	 * @param custid
	 * @return
	 */
	public List<JSONObject> findOrderAndGoods(String custid) {
		
		// 查询已完成的订单
		List<JSONObject> orders = oFormMapper.findOrder(custid);
		

		// 选择不是退货状态的订单
		for (int i = 0; i < orders.size(); i++) {
			String oid = orders.get(i).getString("o_id");
			List<JSONObject> returne = returneMapper.findByOid(oid);
			if (returne != null && returne.size() > 0) {
				orders.remove(i);
			}
		}
		System.out.println(orders);
		return orders;
	}

	/**
	 * 根据订单oid查询订单详情的产品
	 * 
	 * @param oid
	 * @return
	 */
	public List<JSONObject> findGoods(Integer oid) {
		List<JSONObject> goods = detailMapper.findBOid(oid);
		System.out.println(goods);
		return goods;
	}

	/**
	 * 判断商品是否在保质期内
	 * 
	 * @param oid
	 * @param gid
	 * @return
	 */
	public boolean isTimeout(Integer oid, Integer gid) {
		// 查询保质期
		JSONObject deport = gDao.findByGid(gid+"");
		
		
		// 查询订单生成日期
		JSONObject order = oFormMapper.findByIds(oid);
		System.out.println(order);
		
		// 保质期
		Integer ddtime = 0;
		// 订单开始时间
		Date orderTime = null;
		try {
			ddtime = deport.getInteger("ddtime");
			orderTime = order.getDate("o_date");
			// 开始日期
			Calendar start = new GregorianCalendar();
			start.setTime(orderTime);
			start.add(Calendar.WEEK_OF_MONTH, ddtime);// 把日期往后增加一个月.整数往后推,负数往前移动

			// 结束日期
			Calendar end = Calendar.getInstance();
			if (start.before(end)) {
				System.err.println("开始时间小于结束时间");
				return false;
			} else if (start.after(end)) {
				System.err.println("开始时间大于结束时间");
				return true;
			} else if (start.equals(end)) {
				System.err.println("开始时间等于结束时间");
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	/**
	 * 添加图片
	 * @param repId
	 * @param rep_img
	 */
	public boolean addImg(Integer repId, String rep_img) {
		return mapper.addImg(repId,rep_img);
		
	}
	
	/**
	 * 根据图表分类
	 * @return
	 */
	public List<JSONObject> selectByResult1() {
		
		List<JSONObject> list1 =  mapper.selectByResult1();
		List<JSONObject> list2 =  mapper.selectByResult2();
		
		return list1;
	}
}
